package com.phoenix.callAnalysis;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class BaseActivity extends Activity {
	private static final String TAG = BaseActivity.class.getSimpleName();

	protected static SharedPreferences mPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "onCreate...");
		super.onCreate(savedInstanceState);

		mPreferences = CallAnalysisApplication.mPreferences;

		// No Titlebar
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
//		requestWindowFeature(Window.FEATURE_PROGRESS);
	}

	// Menus
	protected static final int OPTIONS_MENU_ID_SETTINGS = 1;
	protected static final int OPTIONS_MENU_ID_ABOUT = 2;
	protected static final int OPTIONS_MENU_ID_EXIT = 3;
	
	@Override
	  public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	    MenuItem item;
	    item = menu.add(0, OPTIONS_MENU_ID_SETTINGS, 0, R.string.omenu_settings);
	    item.setIcon(android.R.drawable.ic_menu_preferences);

	    item = menu.add(0, OPTIONS_MENU_ID_ABOUT, 0, R.string.omenu_about);
	    item.setIcon(android.R.drawable.ic_menu_info_details);
	    
	    item = menu.add(0, OPTIONS_MENU_ID_EXIT, 0, R.string.omenu_exit);
	    item.setIcon(android.R.drawable.ic_menu_revert);

	    return true;
	}
	
	@Override
	  public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case OPTIONS_MENU_ID_SETTINGS:
			
			return true;
		case OPTIONS_MENU_ID_ABOUT:
			
			return true;
		case OPTIONS_MENU_ID_EXIT:
			exit();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		if(this.getClass().getSimpleName().equals("CallAnalysisActivity")) {
			exit();
		} else {
			startActivity(new Intent(this,CallAnalysisActivity.class));
			finish();
		}
	}
	
	private void exit() {
		final AlertDialog.Builder builder = new AlertDialog.Builder(
				this);
		builder.setTitle(R.string.exit_alertDialog_title);
		builder.setCancelable(true);
		builder.setPositiveButton(R.string.yes,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				});
		builder.setNegativeButton(R.string.no,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
			public void onCancel(DialogInterface dialog) {
			}
		});
		builder.setMessage(R.string.exit_alertDialog_message);
		builder.create().show();
	}
}
